﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;

namespace BarManager.Core.Entity
{
    [DataContract]
    public class Orden
    {
        [DataMember]
        public long Id { get; set; }

        [Required(ErrorMessage = "Campo Mesa es obligatorio")]
        [Display(Name = "Mesa")]
        [DataMember]
        public int MesaId { get; set; }

        [Required(ErrorMessage = "Campo Empleado es obligatorio")]
        [Display(Name = "Mozo")]
        [DataMember]
        public int EmpleadoId { get; set; }

        [Required]
        [DataMember]
        public DateTime FechaCreacion { get; set; }

        [DataMember]
        public DateTime? FechaCierre { get; set; }

        [DataMember]
        public EstadoOrden Estado { get; set; }

        [DataMember]
        public decimal? TotalPorPagar
        {
            get { return Detalle == null ? 0 : Detalle.Sum(x => x.SubTotalPorPagar); }
        }

        [DataMember]
        public decimal? TotalPagado
        {
            get { return Detalle == null ? 0 : Detalle.Sum(x => x.SubTotalPagado); }
        }

        [DataMember]
        public decimal? TotalRegalado
        {
            get { return Detalle == null ? 0 : Detalle.Sum(x => x.SubTotalRegalos); }
        }

        [DataMember]
        public decimal? Total
        {
            get { return Detalle == null ? 0 : Detalle.Sum(x => x.SubTotal) - Detalle.Sum(x => x.SubTotalRegalos); }
        }

        [DataMember]
        public int? CantidadTotalRegalos
        {
            get { return Detalle == null ? 0 : Detalle.Sum(x => x.CantidadRegalada); }
        }

        [DataMember]
        public virtual Mesa Mesa { get; set; }

        [DataMember]
        public virtual Empleado Empleado { get; set; }

        [DataMember]
        public virtual ICollection<OrdenDetalleBase> Detalle { get; set; }

        public Orden()
        {
            Estado = EstadoOrden.Abierta;
            FechaCreacion = DateTime.Now;
        }
    }

    public enum EstadoOrden : short
    {
        Abierta = 1,
        Facturada = 2,
        Cancelada = 3
    }
}
